import requests
from bs4 import BeautifulSoup
import json
import os

cateNumer = 300000

# 爬取图书列表页面
def fetch_book_list(url):
    response = requests.get(url)
    if response.status_code == 200:
        soup = BeautifulSoup(response.text, 'html.parser')
        book_links = soup.select('.wr_bookList_item .wr_bookList_item_link')
        return [link['href'] for link in book_links]
    else:
        print(f"Failed to fetch the page: {url}")
        return []

# 爬取图书详情页面
def fetch_book_details(book_url):
    full_url = f"https://weread.qq.com{book_url}"
    response = requests.get(full_url)
    if response.status_code == 200:
        soup = BeautifulSoup(response.text, 'html.parser')
        # 这里你需要根据图书详情页面的结构来提取所需的信息
        urlcover = soup.select_one('.wr_bookCover_img')['src'] if soup.select_one('.wr_bookCover_img') else 'N/A'
        rating = soup.select_one('.book_ratings_header span').text.strip().replace('%', '')  if soup.select_one('.book_ratings_header span') else 'N/A'
        title = soup.select_one('.horizontalReaderCoverPage_content_bookTitle').text.strip() if soup.select_one('.horizontalReaderCoverPage_content_bookTitle') else 'N/A'
        author = soup.select_one('.horizontalReaderCoverPage_content_author').text.strip() if soup.select_one('.horizontalReaderCoverPage_content_author') else 'N/A'
        description = soup.select_one('.horizontalReaderCoverPage_content_bookInfo_intro').text.strip() if soup.select_one('.horizontalReaderCoverPage_content_bookInfo_intro') else 'N/A'
        return {
            'image_url': urlcover,
            'title': title,
            'author': author,
            'rating': rating,
            'description': description

        }
    else:
        print(f"Failed to fetch the book details: {full_url}")
        return {}

# 主函数
def main():
    category_url = 'https://weread.qq.com/web/category/' + str(cateNumer)
    book_links = fetch_book_list(category_url)
    book_details_list = []
    for book_link in book_links:
        book_details = fetch_book_details(book_link)
        if book_details:
            book_details_list.append(book_details)
    
    # 将结果写入到 bookdetail.json 文件中
     # 将书籍信息保存到文件中
    output_dir = os.path.abspath('src/script/books')
    os.makedirs(output_dir, exist_ok=True)
    output_file = os.path.join(output_dir, 'detail_'+ str(cateNumer) +'.json')
    with open(output_file, 'w', encoding='utf-8') as f:
        json.dump(book_details_list, f, ensure_ascii=False, indent=4)
    print(f"Saved {len(book_details_list)} book details to bookdetail.json")

if __name__ == '__main__':
    main()